local FamilyLogicFW = require("family.FamilyLogicFW")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local FamilyManager = require("family.FamilyManager")
local Log = require("common.Log")
local Msg = require("core.Msg")
local Lang = require("common.Lang")
local FamilyDefine = require("family.FamilyDefine")
local Broadcast = require("broadcast.Broadcast")
local HtmlUtil = require("common.HtmlUtil")
local ArmyRiotW = require("activity.armyRiot.ArmyRiotLogicW")
local Util = require("common.Util")
local CopyDefine = require("copy.Define")
local MiddleFamilySign = require("middle.MiddleFamilySign")

function LW_FAMILY_CREATE(human, msg)
    FamilyLogicFW.create(human, msg.createType, msg.familyName)
end


--军团列表
function CG_FAMILY_LIST(human)
    FamilyLogicFW.CG_FAMILY_LIST(human)
end

--军团查询
function CG_FAMILY_QUERY(human, msg)
    FamilyLogicFW.CG_FAMILY_QUERY(human, msg.familyUuid)
end

function CG_FAMILY_LVUP(human, msg)
    FamilyLogicFW.CG_FAMILY_LVUP(human)
end

--申请加入军团
function CG_FAMILY_APPLY(human, msg)
    FamilyLogicFW.CG_FAMILY_APPLY(human, msg.familyUuid)
end

--申请加入军团取消
function CG_FAMILY_APPLY_CANCEL(human, msg)
    FamilyLogicFW.CG_FAMILY_APPLY_CANCEL(human, msg.familyUuid)
end

--查询申请列表
function CG_FAMILY_APPLY_LIST(human)
    FamilyLogicFW.CG_FAMILY_APPLY_LIST(human)
end

--同意某人申请加入军团
function CG_FAMILY_APPLY_AGREE(human, msg)
    FamilyLogicFW.CG_FAMILY_APPLY_AGREE(human, msg.targetUuid, msg.type)
end

--拒绝某人申请加入军团
function CG_FAMILY_APPLY_REFUSE(human, msg)
    FamilyLogicFW.CG_FAMILY_APPLY_REFUSE(human, msg.type, msg.targetname)
end

--邀请加入军团
function CG_FAMILY_INVITE(human, msg)
    FamilyLogicFW.CG_FAMILY_INVITE(human, msg.targetname)
end

--军团推荐列表 针对军团
function CG_FAMILY_COMMEND_LIST(human)
    FamilyLogicFW.CG_FAMILY_COMMEND_LIST(human)
end

--同意某人军团邀请
function CG_FAMILY_INVITE_AGREE(human, msg)
    FamilyLogicFW.CG_FAMILY_INVITE_AGREE(human, msg.familyUuid)
end

--拒绝某人军团邀请
function CG_FAMILY_INVITE_REFUSE(human, msg)
    FamilyLogicFW.CG_FAMILY_INVITE_REFUSE(human, msg.familyUuid, msg.invitorUuid)
end

--查询成员列表
function CG_FAMILY_MEMBER_LIST(human)
    FamilyLogicFW.CG_FAMILY_MEMBER_LIST(human)
end

--退出军团
function CG_FAMILY_EXIT(human)
    FamilyLogicFW.CG_FAMILY_EXIT(human)
end

--T人
function CG_FAMILY_KICK(human, msg)
    FamilyLogicFW.CG_FAMILY_KICK(human, msg.targetUuid)
end

-- 军团官职名查询
function CG_FAMILY_OFFICIAL_NAME_QUERY(human)
    FamilyLogicFW.CG_FAMILY_OFFICIAL_NAME_QUERY(human)
end

--军团官职名改变
function CG_FAMILY_OFFICIAL_NAME_CHANGE(human, msg)
    FamilyLogicFW.CG_FAMILY_OFFICIAL_NAME_CHANGE(human, msg.leader, msg.fu_leader, msg.zhanglao, msg.jingying, msg.normal)
end

--军团qq修改
function CG_FAMILY_QQ_CHANGE(human, msg)
    FamilyLogicFW.CG_FAMILY_QQ_CHANGE(human, msg.qq)
end

--军团sign修改
function CG_FAMILY_SIGN_CHANGE(human, msg)
    FamilyLogicFW.CG_FAMILY_SIGN_CHANGE(human, msg.sign)
end

--招募公告
function CG_FAMILY_RECRUIT_CHANGE(human, msg)
    FamilyLogicFW.CG_FAMILY_RECRUIT_CHANGE(human, msg.sign)
end

-- 禅让
function CG_FAMILY_LEADER_CHANGE(human, msg)
    FamilyLogicFW.CG_FAMILY_LEADER_CHANGE(human, msg.newLeaderUuid)
end

--官职任命
function CG_FAMILY_OFFICIAL_APPOINT(human, msg)
    FamilyLogicFW.CG_FAMILY_OFFICIAL_APPOINT(human, msg.targetUuid, msg.official)
end


-- 军团仓库查询
function CG_FAMILY_STORE_LIST(human)
    FamilyLogicFW.CG_FAMILY_STORE_LIST(human)
end

-- 查询军团仓库记录
function CG_FAMILY_STORE_RECORD_QUERY(human)
    FamilyLogicFW.CG_FAMILY_STORE_RECORD_QUERY(human)
end

-- 军团仓库格子open
function CG_FAMILY_STORE_ITEM_OPEN(human, msg)
    FamilyLogicFW.CG_FAMILY_STORE_ITEM_OPEN(human, msg.storeIndex, msg.state)
end

-- 军团仓库格子交换
function CG_FAMILY_STORE_ITEM_SWAP(human, msg)
    FamilyLogicFW.CG_FAMILY_STORE_ITEM_SWAP(human, msg.srcIndex, msg.tarIndex)
end

-- 军团仓库整理
function CG_FAMILY_STORE_SORT(human)
    FamilyLogicFW.CG_FAMILY_STORE_SORT(human)
end



function CG_FAMILY_BANNER_UP(human)
    FamilyLogicFW.CG_FAMILY_BANNER_UP(human)
end


--军团招募
function CG_FAMILY_RECRUIT(human)
    FamilyLogicFW.CG_FAMILY_RECRUIT(human)
end


function CG_FAMILY_CONTRIBUTE_QUERY(human)
    FamilyLogicFW.CG_FAMILY_CONTRIBUTE_QUERY(human)
end

function CG_FAMILY_EXTEND_OPEN_QUERY(human)
    FamilyLogicFW.CG_FAMILY_EXTEND_OPEN_QUERY(human)
end

function CG_FAMILY_EXTEND(human)
    FamilyLogicFW.CG_FAMILY_EXTEND(human)
end

function CG_FAMILY_ALLY(human, msg)
    FamilyLogicFW.CG_FAMILY_ALLY(human, msg.familyUuid)
end

function CG_FAMILY_ALLY_AGREE(human, msg)
    FamilyLogicFW.CG_FAMILY_ALLY_AGREE(human, msg.familyUuid)
end

function CG_FAMILY_ALLY_REFUSE(human, msg)
    FamilyLogicFW.CG_FAMILY_ALLY_REFUSE(human, msg.familyUuid)
end

function CG_FAMILY_ALLY_BREAK(human)
    FamilyLogicFW.CG_FAMILY_ALLY_BREAK(human)
end


function CG_FAMILY_AUTO_APPLY_AGREE(human, msg)
    FamilyLogicFW.CG_FAMILY_AUTO_APPLY_AGREE(human, msg.state)
end

function CG_FAMILY_ACT_COPY_START(human, msg)
    FamilyLogicFW.CG_FAMILY_ACT_COPY_START(human, 1)
end

function CG_FAMILY_WAR_JOIN(human, msg)
    FamilyLogicFW.CG_FAMILY_WAR_JOIN(human, msg.uuid)
end

function CG_FAMILY_WAR_REWARD_QUERY(human, msg)
    MiddleFamilySign.rewardQuery(human)
end

function CG_FAMILY_WAR_SIGN_QUERY(human, msg)
    MiddleFamilySign.signQuery(human)
end

function CG_FAMILY_WAR_SIGN(human, msg)
    MiddleFamilySign.sign(human)
end

function CG_FAMILY_ACT_TY_QUERY(human, msg)
    FamilyLogicFW.sendFamilyTaoyuanData(human)
end

function CG_FAMILY_ACT_TY_REWARD(human, msg)
    FamilyLogicFW.familyTaoyuanGetReward(human, msg.index)
end

function L2WProcess(human, oType, oData)
    if oData.type == 3 then         --军团聊天的处理
        local family = FamilyManager.findByUuid(human.familyUuid)
        if not family then
            return
        end

        local fds = FamilyManager.getOnlineFds(family)
        Msg.sendMulti(oData.msg, fds)
        --Log.write(Log.LOGID_OSS_FAMILY_CHAT, human.db._id, human.db.account, human.db.name, human.db.lv, family.familyName, oData.msg)
    elseif oData.type == 5 then     --仓库存放
        local nGridData = oData.data.bagGrid
        if not nGridData then
            return
        end

        local msgErr = Msg.gc.GC_BROADCAST
        msgErr.type = Lang.MOUSE
        local family = FamilyManager.findByUuid(human.familyUuid)
        if not family then
            msgErr.msg = Lang.FAMILY_NOT_EXIST
            Msg.send(msgErr, human.fd)
            return
        end

        if not FamilyLogicFW.checkCanAddItem(family, nGridData) then
            msgErr.msg = Lang.FAMILY_STORE_CAP_NOT_ENOUGH
            Msg.send(msgErr, human.fd)
            return
        end

        if FamilyLogicFW.familyStoreAddItem(family, human, oData) then
            local nSend2Ldata = {
                type = 5,
                data = {result = 0, bagIndex = oData.data.bagIndex}
            }
            InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_FAMILY, nSend2Ldata)
        end
    elseif oData.type == 6 then     --仓库提取
        local msgErr = Msg.gc.GC_BROADCAST
        msgErr.type = Lang.MOUSE
        local storeIndex = oData.data.storeIndex
        local cnt = oData.data.cnt
        local family = FamilyManager.findByUuid(human.familyUuid)
        if not family then
            msgErr.msg = Lang.FAMILY_NOT_EXIST
            Msg.send(msgErr, human.fd)
            return
        end

        local storeIndexOK = FamilyLogicFW.checkIndex(family, storeIndex)
        if not storeIndexOK then
            msgErr.msg = Lang.FAMILY_STORE_INDEX_INVALID
            Msg.send(msgErr, human.fd)
            return
        end

        local storeGrid = family.store[storeIndex]

        if not storeGrid then
            msgErr.msg = Lang.FAMILY_STORE_INDEX_INVALID
            Msg.send(msgErr, human.fd)
            return
        end

        if cnt < 1 or cnt > storeGrid.cnt then
            msgErr.msg = Lang.FAMILY_STORE_GET_CNT_INVALID
            Msg.send(msgErr, human.fd)
            return
        end

        local isOfficial = FamilyManager.isOfficial(family, FamilyDefine.OFFICIAL_LEADER, human._id) or FamilyManager.isOfficial(family, FamilyDefine.OFFICIAL_FU_LEADER, human._id)
        if not isOfficial then
            if storeGrid.open == 0 then
                msgErr.msg = Lang.FAMILY_NOT_LEADER
                Msg.send(msgErr, human.fd)
                return
            end
        end

        --if not isOfficial and cnt ~= 1 then
        --    msgErr.msg = Lang.FAMILY_STORE_GET_CNT_INVALID
        --    Msg.send(msgErr, human.fd)
        --    return
        --end

        local newStoreGrid = {}
        Util.copyTable(storeGrid, newStoreGrid)

        if FamilyLogicFW.familyStoreDelItem(family, human, storeGrid, cnt, storeIndex) then
            newStoreGrid.cnt = cnt
            local nSend2Ldata = {
                type = 6,
                data = {result = 0, bagIndex = oData.data.bagIndex, griddata = newStoreGrid}
            }
            InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_FAMILY, nSend2Ldata)
        end
    elseif oData.type == 7 then         --军团捐献
        local msgErr = Msg.gc.GC_BROADCAST
        msgErr.type = Lang.MOUSE

        local family = FamilyManager.findByUuid(human.familyUuid)
        if not family then
            return
        end

        FamilyLogicFW.setFamilyContribution(human, oData.data.addcontribution)
        FamilyLogicFW.sendContribution(human, family)

        local fds = FamilyManager.getOnlineFds(family)
        Broadcast.sendBroadcastFDs(fds, Lang.FAMILY, string.format(Lang.FAMILY_CONTRIBUTE_BROADCAST, HtmlUtil.fontName(human.name, human._id)))

        family.familyMoney = family.familyMoney + oData.data.addmoney
        onAddFamilyExp(human, oData)
    elseif oData.type == 9 then         --军团军旗捐献
        local msgErr = Msg.gc.GC_BROADCAST
        msgErr.type = Lang.MOUSE

        local family = FamilyManager.findByUuid(human.familyUuid)
        if not family then
            return
        end

        FamilyLogicFW.addBannerItemNum(human, family, oData.data.itemData)

        FamilyLogicFW.setFamilyContribution(human, oData.data.addcontribution)
        FamilyLogicFW.sendContribution(human, family)

        local fds = FamilyManager.getOnlineFds(family)
        Broadcast.sendBroadcastFDs(fds, Lang.FAMILY, string.format(Lang.FAMILY_CONTRIBUTE_FLAG_BROADCAST, HtmlUtil.fontName(human.name, human._id)))

        FamilyLogicFW.sendFamilyUpdate(family)
        --Log.write(Log.LOGID_OSS_FAMILY_CONTRIBUTE, human._id, human.db.account, human.name, human.db.lv, 0, yinliang, addContribution, human.db.familyContribution, family._id, family.familyName, family.familyLv)
    elseif oData.type == 10 then
        if onAddFamilyExp(human, oData) then
            ArmyRiotW.setRiotFamilyExp(human, oData.data.addexp)
        end
    elseif oData.type == 11 then
        local family = FamilyManager.findByUuid(human.familyUuid)
        if family then
            local msg = Msg.gc.GC_FAMILY_ACT_COPY_LEFT
            msg.copyID = CopyDefine.COPY_FAMILY_COPY_ID[1]
            msg.leftTimes = 2 - family.familyCopyTimes
            msg.yb = FamilyDefine.FAMILY_COPY_YUANBAO
            msg.money = FamilyDefine.FAMILY_COPY_MONEY
            msg.isOpen = FamilyLogicFW.checkFamilyCopyIsOpen(family._id, msg.copyID) and 1 or 0
            Msg.send(msg, human.fd)
        end  
        
    elseif oData.type == 12 then
        local family = FamilyManager.findByUuid(human.familyUuid)
        if family then
            FamilyLogicFW.CG_FAMILY_ACT_COPY_START(human, 2)
        end 
    elseif oData.type == 13 then
        local family = FamilyManager.findByUuid(human.familyUuid)
        if family then
            FamilyLogicFW.familyTaoyuanGive(human, family, oData.free)
        end  
    elseif oData.type == -1 then
        local nFamilyName = human.familyName
        local family = FamilyManager.findByFamilyName(nFamilyName)
        if family then
            FamilyLogicFW.kickallmember(family)
        end
    end
end

function onAddFamilyExp(human, oData)
    local msgErr = Msg.gc.GC_BROADCAST
    msgErr.type = Lang.MOUSE

    local family = FamilyManager.findByUuid(human.familyUuid)
    if not family or oData.data.addexp <= 0 then
        return
    end

    --军团经验
    local addExp = FamilyLogicFW.getAddFamilyExp(family, oData.data.addexp)
    if addExp == 0 then
        msgErr.type = Lang.MOUSE
        msgErr.msg = Lang.FAMILY_CONTRIBUT_EXP_MAX
        Msg.send(msgErr, human.fd)
    else
        FamilyLogicFW.addFamilyExp(family, oData.data.addexp, nil, human)
    end

    if not family.dailyFamilyExp then
        family.dailyFamilyExp = 0
    end

    family.dailyFamilyExp = family.dailyFamilyExp + addExp
    FamilyLogicFW.sendFamilyUpdate(family)
    Log.write(Log.LOGID_OSS_FAMILY_INFO, family._id, family.familyName, human.name, FamilyManager.getLeader(family).name or "", family.familyLv, family.familyMoney, family.familyBanner)
    return true
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_FAMILY, L2WProcess)
